package search2;

import agents.AutomatonAgent;
import env.Action;
import env.Enviroment;
import env.Orientation;
import env.Tile;
import java.util.Vector;

/**
 * TODO: ?? use this class instead of passing around row, col, and orientation???
 * @author user
 */
public class State2 {
    AgentState myState;
    AgentState sentryState;
    private Vector<Tile> flags; //available flags
    //public boolean capture = false;
    boolean isAlive;

    public State2(AgentState myState, AgentState sentryState, Vector<Tile> flags, boolean isAlive) {
        this.myState=myState;
        this.sentryState=sentryState;//new AgentState(sentry.getRow(), sentry.getCol(), sentry.getOrientation());
        this.flags = flags; 
        this.isAlive=isAlive;
    }

    State2 clone(Action move) {
        final AgentState nextMyState = myState.clone(move);
        final Tile nextTile = nextMyState.getTile();
        final Vector<Tile> nextFlags;
        if (flags.contains(nextTile)) {
                nextFlags = new Vector<Tile>(flags.size()-1);
                for (Tile f : flags) {
                    if (f != nextTile) {
                        nextFlags.add(f);
                    }
                }
            }
        else nextFlags=flags;
        //TODO: sentry factory
        final AgentState nextSentryState = sentryState.clone(AutomatonAI.nextMove(sentryState));
        boolean nextIsAlive=//sentryState.getTile().hasLOSwith(nextSentryState.getTile());
            !Enviroment.env.isInLineOfSight(nextMyState.getRow(), nextMyState.getCol(), sentryState.getRow(), sentryState.getCol());
          //TODO: is that the right way to check in the sequential system?
        return new State2(nextMyState, nextSentryState, nextFlags,nextIsAlive);
    }

    public Tile getTile(){
        return myState.getTile();
    }

    Vector<Tile> getFlags() {
        return flags;
    }

    @Override
    public boolean equals(Object obj) {
        final State2 other = (State2) obj;
        if (this.myState != other.myState && !this.myState.equals(other.myState)) {
            return false;
        }
        if (this.sentryState != other.sentryState && !this.sentryState.equals(other.sentryState)) {
            return false;
        }
        if (this.flags != other.flags && !this.flags.equals(other.flags)) {
            return false;
        }
//        if (this.isAlive != other.isAlive) {
//            return false;
//        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 29 * hash + this.myState.hashCode();
        hash = 29 * hash + this.sentryState.hashCode();
        hash = 29 * hash + this.flags.hashCode();
//        hash = 29 * hash + (this.isAlive ? 1 : 0);
        return hash;
    }
    
    

}
